package org.midy.base;

import jdk.nashorn.internal.ir.CallNode;
import org.midy.infrastructure.tools.Logger;
import org.midy.infrastructure.tools.ThreadTools;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ClientMain {
    public static void main(String[] args) {
        final ClientLongConnectionPool clientLongConnectionPool = ClientLongConnectionPool.build(1, 5, 100000, "10.252.16.219", 5058, 8, "UTF-8");
//        int nThread = 1;
//        int msgCount = 100;
//        int taskNum = 1;
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(nThread, nThread, 100, TimeUnit.HOURS, new LinkedBlockingQueue<>());

        System.out.println("hh ");
        try {
            String response = clientLongConnectionPool.sendAndReceive("你好啊我是米大洋");
            System.out.println("接受到消息：" + response);
            response = clientLongConnectionPool.sendAndReceive("你们在干啥呢");
            System.out.println("接受到消息：" + response);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

//        threadPoolExecutor.prestartAllCoreThreads();
//        final CountDownLatch mainWaiting = new CountDownLatch(taskNum);
//        for (int idx = 0; idx < taskNum; idx++) {
//            threadPoolExecutor.execute(() -> {
//                long start = System.currentTimeMillis();
//                try {
//                    int currMsgCount = 0;
//                    String threadName = Thread.currentThread().getName();
//                    String response = null;
//                    while (currMsgCount++ < msgCount) {
//                        try {
//                            String request = String.format("type: request, name: %s, currTotal: %d", threadName, currMsgCount);
//                            response = clientLongConnectionPool.sendAndReceive(request);
//                        } catch (Throwable t) {
//                            Logger.error("接受和发送消息失败", t);
//                        }
////                        finally {
//////                            Logger.info("name:[%s], count:[%d], request:[%s], response:[%s]", threadName, currMsgCount, request, response);
////                        }
////                        ThreadTools.sleepRandom(80000);
//                    }
//                } catch (Throwable e) {
//                    Logger.error("线程任务执行失败！", e);
//                } finally {
//                    Logger.info(" count: %s, cost: %d", msgCount, (System.currentTimeMillis() - start));
//                    mainWaiting.countDown();
//                }
//            });
        }

//        while (mainWaiting.getCount() > 0) {
//            try {
//                mainWaiting.await();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }

//        threadPoolExecutor.shutdown();
    }
//}
